import 'package:bxultimate/domain/common/result.dart';
import 'package:bxultimate/shared/shared.dart';
import 'package:get/get.dart';
import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/request/user_request.dart';
import 'package:bxultimate/data/store/auth_store.dart';
import 'package:bxultimate/domain/auth/login_token.dart';
import 'package:bxultimate/shared/util/util_mk.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'state.dart';

class AuthRegisterController extends GetxController {
  final AuthRegisterState state = AuthRegisterState();

  final TextEditingController phoneController = TextEditingController();
  final TextEditingController codeController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();
  final TextEditingController rePasswordController = TextEditingController();
  final FocusNode nodeText1 = FocusNode();
  final FocusNode nodeText2 = FocusNode();
  final FocusNode nodeText3 = FocusNode();
  final FocusNode nodeText4 = FocusNode();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    phoneController.addListener(_verify);
    codeController.addListener(_verify);
    passwordController.addListener(_verify);
    rePasswordController.addListener(_verify);
  }

  /**
   * 注册检查
   */
  void registerCheck() {
    if (state.checkboxIsCheck) {
      register();
      return;
    }
    TextStyle style =
        TextStyle(fontSize: 16, color: TDTheme.of(Get.context!).brandColor5);
    UtilMk.openBottomSheet(
        200,
        "请阅读并同意以下条款",
        SafeArea(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Gaps.vGap24,
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              GestureDetector(
                onTap: () =>
                    {Get.toNamed(RouterName.userSystemSettingUserAgreement)},
                child: Text(
                  "《用户协议》",
                  style: style,
                ),
              ),
              GestureDetector(
                onTap: () => {Get.toNamed(RouterName.userSystemSettingPrivacy)},
                child: Text(
                  "《隐私政策》",
                  style: style,
                ),
              ),
            ]),
            Gaps.vGap24,
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 20),
              child: TDButton(
                theme: TDButtonTheme.primary,
                onTap: continueStep,
                text: "同意并注册",
                isBlock: true,
              ),
            )
          ],
        )));
  }

  void continueStep() {
    state.checkboxIsCheck = true;
    Get.back();
    register();
  }

  Future<void> register() async {
    if (passwordController.text != rePasswordController.text) {
      SmartDialog.showToast('两次密码输入不一致');
      return;
    }

    // 判断密码复杂度
    if (!UtilValidate.checkPassword(passwordController.text)) {
      SmartDialog.showToast('密码复杂度不够');
      return;
    }

    SmartDialog.showLoading(msg: '注册中...');
    Result result = await UserRequest.register(
        phoneController.text,
        phoneController.text,
        codeController.text,
        passwordController.text);
    SmartDialog.dismiss();
    if (result.code == AppConstant.RESULT_ERROR) {
      SmartDialog.showToast(result.message!);
      return;
    }
    SmartDialog.showNotify(msg: '注册成功', notifyType: NotifyType.success);
    Get.back();
  }

  void _verify() {
    final String name = phoneController.text;
    final String code = codeController.text;
    final String password = passwordController.text;
    final String rePassword = rePasswordController.text;
    if (name.isEmpty || name.length < 11) {
      state.clickable = false;
    } else if (code.isEmpty || code.length < 4) {
      state.clickable = false;
    } else if (password.isEmpty || password.length < 4) {
      state.clickable = false;
    } else if (rePassword.isEmpty || rePassword.length < 4) {
      state.clickable = false;
    } else {
      state.clickable = true;
    }
  }

  void toSmsLogin() {
    Get.toNamed(RouterName.userLoginSms);
  }

  checkBoxCheck(bool checked) {
    state.checkboxIsCheck = checked;
  }

  Future<bool> getVCode() async {
    if (!UtilValidate.checkPhone(phoneController.text)) {
      SmartDialog.showToast('请输入正确的手机号');
      return false;
    }
    Result result = await UserRequest.sendRegisterSmsCode(phoneController.text);
    if (result.code == AppConstant.RESULT_ERROR) {
      SmartDialog.showToast(result.message!);
      return false;
    } else {
      SmartDialog.showToast('验证码已发送');
      return true;
    }
  }
}
